#!/bin/sh
echo "开始备份数据库";

backup_dir=/data/backup/mysql
db_name=my_db
db_container=mysql

docker exec  ${db_container} sh -c 'exec mysqldump -uroot -proot ${db_name}' | gzip > "${backup_dir}/${db_name}_$(hostname)_$(date +%Y-%m-%d_%H%M%S).sql.gz"

find "$backup_dir" -name "${db_name}*.sql.gz" -type f -mtime +300 -exec rm -fv {} \; >> "${backup_dir}/backup_clean.log" 2>&1

echo "备份完成";

# sudo mkdir /data/backup/mysql -p
# sudo chmod a+w /data/backup/mysql
# sudo chmod +x /data/deploy/backup_mysql.sh
# crontab -e
# 0 */3 * * *  /data/deploy/backup_mysql.sh

# gunzip -c /data/backup/mysql/${db_name}_$(hostname)_date.sql.gz | docker exec -i ${db_container} mysql -uroot -proot ${db_name}
